// AnnoDialog.cpp : implementation file
//

#include "stdafx.h"
#include "EJWork.h"
#include "AnnoDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAnnoDialog dialog


CAnnoDialog::CAnnoDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CAnnoDialog::IDD, pParent)
{
	int i;
	//{{AFX_DATA_INIT(CAnnoDialog)
	//}}AFX_DATA_INIT
	i = 0;
	m_wndCls[i++] = &m_wndCls0;
	m_wndCls[i++] = &m_wndCls1;
	m_wndCls[i++] = &m_wndCls2;
	m_wndCls[i++] = &m_wndCls3;
	m_wndCls[i++] = &m_wndCls4;
	m_wndCls[i++] = &m_wndCls5;
	m_wndCls[i++] = &m_wndCls6;
	m_wndCls[i++] = &m_wndCls7;
	m_wndCls[i++] = &m_wndCls8;
	m_wndCls[i++] = &m_wndCls9;
	
	i = 0;
	m_wndDat[i++] = &m_wndDat0;
	m_wndDat[i++] = &m_wndDat1;
	m_wndDat[i++] = &m_wndDat2;
	m_wndDat[i++] = &m_wndDat3;
	m_wndDat[i++] = &m_wndDat4;
	m_wndDat[i++] = &m_wndDat5;
	m_wndDat[i++] = &m_wndDat6;
	m_wndDat[i++] = &m_wndDat7;
	m_wndDat[i++] = &m_wndDat8;
	m_wndDat[i++] = &m_wndDat9;
	
	page = NULL;

	m_nCls = -1;
	annoIndex = 0;
}


void CAnnoDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAnnoDialog)
	DDX_Control(pDX, IDC_EDIT_DAT9, m_wndDat9);
	DDX_Control(pDX, IDC_EDIT_DAT8, m_wndDat8);
	DDX_Control(pDX, IDC_EDIT_DAT7, m_wndDat7);
	DDX_Control(pDX, IDC_EDIT_DAT6, m_wndDat6);
	DDX_Control(pDX, IDC_EDIT_DAT5, m_wndDat5);
	DDX_Control(pDX, IDC_EDIT_CLS9, m_wndCls9);
	DDX_Control(pDX, IDC_EDIT_CLS8, m_wndCls8);
	DDX_Control(pDX, IDC_EDIT_CLS7, m_wndCls7);
	DDX_Control(pDX, IDC_EDIT_CLS6, m_wndCls6);
	DDX_Control(pDX, IDC_EDIT_CLS5, m_wndCls5);
	DDX_Control(pDX, IDC_EDIT_TEXT, m_wndText);
	DDX_Control(pDX, IDC_PREVIEW, m_annoWnd);
	DDX_Control(pDX, IDC_LIST_DAT, m_lvDat);
	DDX_Control(pDX, IDC_LIST_CLS, m_lvCls);
	DDX_Control(pDX, IDC_EDIT_DAT4, m_wndDat4);
	DDX_Control(pDX, IDC_EDIT_DAT3, m_wndDat3);
	DDX_Control(pDX, IDC_EDIT_DAT2, m_wndDat2);
	DDX_Control(pDX, IDC_EDIT_DAT1, m_wndDat1);
	DDX_Control(pDX, IDC_EDIT_DAT0, m_wndDat0);
	DDX_Control(pDX, IDC_EDIT_CLS4, m_wndCls4);
	DDX_Control(pDX, IDC_EDIT_CLS3, m_wndCls3);
	DDX_Control(pDX, IDC_EDIT_CLS2, m_wndCls2);
	DDX_Control(pDX, IDC_EDIT_CLS1, m_wndCls1);
	DDX_Control(pDX, IDC_EDIT_CLS0, m_wndCls0);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAnnoDialog, CDialog)
	//{{AFX_MSG_MAP(CAnnoDialog)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS0, OnKillfocusEditCls0)
	ON_EN_SETFOCUS(IDC_EDIT_CLS0, OnSetfocusEditCls0)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS1, OnKillfocusEditCls1)
	ON_EN_SETFOCUS(IDC_EDIT_CLS1, OnSetfocusEditCls1)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS2, OnKillfocusEditCls2)
	ON_EN_SETFOCUS(IDC_EDIT_CLS2, OnSetfocusEditCls2)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS3, OnKillfocusEditCls3)
	ON_EN_SETFOCUS(IDC_EDIT_CLS3, OnSetfocusEditCls3)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS4, OnKillfocusEditCls4)
	ON_EN_SETFOCUS(IDC_EDIT_CLS4, OnSetfocusEditCls4)
	ON_LBN_SELCHANGE(IDC_LIST_CLS, OnSelchangeListCls)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT0, OnKillfocusEditDat0)
	ON_EN_SETFOCUS(IDC_EDIT_DAT0, OnSetfocusEditDat0)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT1, OnKillfocusEditDat1)
	ON_EN_SETFOCUS(IDC_EDIT_DAT1, OnSetfocusEditDat1)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT2, OnKillfocusEditDat2)
	ON_EN_SETFOCUS(IDC_EDIT_DAT2, OnSetfocusEditDat2)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT3, OnKillfocusEditDat3)
	ON_EN_SETFOCUS(IDC_EDIT_DAT3, OnSetfocusEditDat3)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT4, OnKillfocusEditDat4)
	ON_EN_SETFOCUS(IDC_EDIT_DAT4, OnSetfocusEditDat4)
	ON_LBN_DBLCLK(IDC_LIST_DAT, OnDblclkListDat)
	ON_EN_CHANGE(IDC_EDIT_TEXT, OnChangeEditText)
	ON_WM_KEYUP()
	ON_EN_KILLFOCUS(IDC_EDIT_CLS5, OnKillfocusEditCls5)
	ON_EN_SETFOCUS(IDC_EDIT_CLS5, OnSetfocusEditCls5)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS6, OnKillfocusEditCls6)
	ON_EN_SETFOCUS(IDC_EDIT_CLS6, OnSetfocusEditCls6)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS7, OnKillfocusEditCls7)
	ON_EN_SETFOCUS(IDC_EDIT_CLS7, OnSetfocusEditCls7)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS8, OnKillfocusEditCls8)
	ON_EN_SETFOCUS(IDC_EDIT_CLS8, OnSetfocusEditCls8)
	ON_EN_KILLFOCUS(IDC_EDIT_CLS9, OnKillfocusEditCls9)
	ON_EN_SETFOCUS(IDC_EDIT_CLS9, OnSetfocusEditCls9)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT5, OnKillfocusEditDat5)
	ON_EN_SETFOCUS(IDC_EDIT_DAT5, OnSetfocusEditDat5)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT6, OnKillfocusEditDat6)
	ON_EN_SETFOCUS(IDC_EDIT_DAT6, OnSetfocusEditDat6)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT7, OnKillfocusEditDat7)
	ON_EN_SETFOCUS(IDC_EDIT_DAT7, OnSetfocusEditDat7)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT8, OnKillfocusEditDat8)
	ON_EN_SETFOCUS(IDC_EDIT_DAT8, OnSetfocusEditDat8)
	ON_EN_KILLFOCUS(IDC_EDIT_DAT9, OnKillfocusEditDat9)
	ON_EN_SETFOCUS(IDC_EDIT_DAT9, OnSetfocusEditDat9)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnnoDialog message handlers

BOOL CAnnoDialog::OnInitDialog() 
{
	WORD i;

	CDialog::OnInitDialog();

	m_bModified = 0;
	
	RefreshCls();

	if (page)
	{
		if (annoIndex>=0 && annoIndex<page->num)
		{
			const CEJAnno& anno = page->anno[annoIndex];

			for(i = 0;i<NUM_ANNO_ITEM;i++)
			{
				m_wndCls[i]->SetWindowText(anno.item[i].cls);
				m_wndDat[i]->SetWindowText(anno.item[i].dat);
			}

			m_wndText.SetWindowText(anno.text);
		}
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CAnnoDialog::RefreshCls()
{
	int i;
	
	m_lvCls.ResetContent();
	for(i = 0;i<theApp.anno.num;i++)
	{
		m_lvCls.AddString(theApp.anno.item[i].cls);
	}

	if (m_nCls>=0 && m_nCls<m_lvCls.GetCount())
	{
		m_lvCls.SetCurSel(m_nCls);
		OnSelchangeListCls();
	}
}

void CAnnoDialog::BeginEdit(WORD index)
{
	WORD clsIndex;
	WORD datIndex;
	CString cls;
	CString dat;

	m_nItemIndex = index;

	m_wndCls[index]->GetWindowText(cls);
	m_wndDat[index]->GetWindowText(dat);

	cls.TrimLeft();
	cls.TrimRight();
	dat.TrimLeft();
	dat.TrimRight();

	if (!cls.IsEmpty() && !dat.IsEmpty())
	{
		if (theApp.anno.IsContain(cls,dat,&clsIndex,&datIndex))
		{
			m_lvCls.SetCurSel(clsIndex);
			OnSelchangeListCls();
			m_lvDat.SetCurSel(datIndex);
		}
	}
}

void CAnnoDialog::EndEdit(WORD index)
{
	WORD clsIndex;
	WORD datIndex;
	CString cls;
	CString dat;

	m_nItemIndex = index;
	
	m_wndCls[index]->GetWindowText(cls);
	m_wndDat[index]->GetWindowText(dat);
	
	cls.TrimLeft();
	cls.TrimRight();
	dat.TrimLeft();
	dat.TrimRight();
	
	if (!cls.IsEmpty() && !dat.IsEmpty())
	{
		if (theApp.anno.IsContain(cls,dat,&clsIndex,&datIndex))
		{
			m_lvDat.SetCurSel(datIndex);
		}
		else
		{
			theApp.anno.Add(cls,dat);
			RefreshCls();
		}
		
	}
	CEJAnno& anno = page->anno[annoIndex];
	anno.item[index].cls = cls;
	anno.item[index].dat = dat;
	
	m_bModified = 1;
}

void CAnnoDialog::OnKillfocusEditCls0() 
{
	EndEdit(0);	
}

void CAnnoDialog::OnSetfocusEditCls0() 
{
	BeginEdit(0);
}

void CAnnoDialog::OnKillfocusEditCls1() 
{
	EndEdit(1);
}

void CAnnoDialog::OnSetfocusEditCls1() 
{
	BeginEdit(1);
}

void CAnnoDialog::OnKillfocusEditCls2() 
{
	EndEdit(2);
}

void CAnnoDialog::OnSetfocusEditCls2() 
{
	BeginEdit(2);
}

void CAnnoDialog::OnKillfocusEditCls3() 
{
	EndEdit(3);	
}

void CAnnoDialog::OnSetfocusEditCls3() 
{
	BeginEdit(3);
}

void CAnnoDialog::OnKillfocusEditCls4() 
{
	EndEdit(4);
}

void CAnnoDialog::OnSetfocusEditCls4() 
{
	BeginEdit(4);
}

void CAnnoDialog::OnSelchangeListCls() 
{
	int i;
	int index = m_lvCls.GetCurSel();

	if (index>=0 && index<m_lvCls.GetCount())
	{
//		if (index!=m_nCls)
		{
			m_nCls = index;

			CAnnoTmplItem& ti = theApp.anno.item[index];
			
			m_lvDat.ResetContent();
			for(i = 0;i<ti.dat.GetSize();i++)
			{
				m_lvDat.AddString(ti.dat[i]);
			}
		}
	}
}

void CAnnoDialog::OnKillfocusEditDat0() 
{
	EndEdit(0);
}

void CAnnoDialog::OnSetfocusEditDat0() 
{
	BeginEdit(0);
}

void CAnnoDialog::OnKillfocusEditDat1() 
{
	EndEdit(1);
}

void CAnnoDialog::OnSetfocusEditDat1() 
{
	BeginEdit(1);
}

void CAnnoDialog::OnKillfocusEditDat2() 
{
	EndEdit(2);
}

void CAnnoDialog::OnSetfocusEditDat2() 
{
	BeginEdit(2);
}

void CAnnoDialog::OnKillfocusEditDat3() 
{
	EndEdit(3);
}

void CAnnoDialog::OnSetfocusEditDat3() 
{
	BeginEdit(3);
}

void CAnnoDialog::OnKillfocusEditDat4() 
{
	EndEdit(4);	
}

void CAnnoDialog::OnSetfocusEditDat4() 
{
	BeginEdit(4);
}

void CAnnoDialog::OnDblclkListDat() 
{
	int index;
	WORD itemIndex;
	CString cls;
	CString dat;

	if (m_nCls>=0 && m_nCls<m_lvCls.GetCount())
	{
		m_lvCls.GetText(m_nCls,cls);

		index = m_lvDat.GetCurSel();
		m_lvDat.GetText(index,dat);
		
		if (index>=0 && index<m_lvDat.GetCount())
		{
			CEJAnno& anno = page->anno[annoIndex];

			if (!anno.FindByCls(cls,&itemIndex))
			{
				if (!anno.FindEmpty(&itemIndex))
				{
					itemIndex = m_nItemIndex;
				}
			}

			anno.item[itemIndex].cls = cls;
			anno.item[itemIndex].dat = dat;

			m_wndCls[itemIndex]->SetWindowText(cls);
			m_wndDat[itemIndex]->SetWindowText(dat);

			m_bModified = 1;
		}
	}
}

void CAnnoDialog::OnChangeEditText() 
{
	CString str;

	CEJAnno& anno = page->anno[annoIndex];
	m_wndText.GetWindowText(str);

	str.TrimLeft();
	str.TrimRight();

	if (anno.text.CompareNoCase(str))
	{
		anno.text = str;
		m_bModified = 1;
	}
}

void CAnnoDialog::OnKillfocusEditCls5() 
{
	EndEdit(5);
}

void CAnnoDialog::OnSetfocusEditCls5() 
{
	BeginEdit(5);
}

void CAnnoDialog::OnKillfocusEditCls6() 
{
	EndEdit(6);
}

void CAnnoDialog::OnSetfocusEditCls6() 
{
	BeginEdit(6);
}

void CAnnoDialog::OnKillfocusEditCls7() 
{
	EndEdit(7);
}

void CAnnoDialog::OnSetfocusEditCls7() 
{
	BeginEdit(7);
}

void CAnnoDialog::OnKillfocusEditCls8() 
{
	EndEdit(8);
}

void CAnnoDialog::OnSetfocusEditCls8() 
{
	BeginEdit(8);
}

void CAnnoDialog::OnKillfocusEditCls9() 
{
	EndEdit(9);
}

void CAnnoDialog::OnSetfocusEditCls9() 
{
	BeginEdit(9);
}

void CAnnoDialog::OnKillfocusEditDat5() 
{
	EndEdit(5);
}

void CAnnoDialog::OnSetfocusEditDat5() 
{
	BeginEdit(5);	
}

void CAnnoDialog::OnKillfocusEditDat6() 
{
	EndEdit(6);
}

void CAnnoDialog::OnSetfocusEditDat6() 
{
	BeginEdit(6);	
}

void CAnnoDialog::OnKillfocusEditDat7() 
{
	EndEdit(7);	
}

void CAnnoDialog::OnSetfocusEditDat7() 
{
	BeginEdit(7);
}

void CAnnoDialog::OnKillfocusEditDat8() 
{
	EndEdit(8);
}

void CAnnoDialog::OnSetfocusEditDat8() 
{
	BeginEdit(8);
}

void CAnnoDialog::OnKillfocusEditDat9() 
{
	EndEdit(9);
}

void CAnnoDialog::OnSetfocusEditDat9() 
{
	BeginEdit(9);
}
